package com.rockcent.wechat.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.Location;
import com.rockcent.mall.lib.dto.LocationDto;
import com.rockcent.mall.lib.repository.LocationRepository;
import com.rockcent.wechat.Routes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/17.
 */
@RestController
@RequestMapping(value = Routes.API_PREFIX)
public class LocationController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CustomerWalletController.class);
    @Autowired
    private LocationRepository locationRepository;

    /**
     * @api {get} /api/v2/location/list 地区列表
     * @apiGroup location
     * @apiVersion 1.0.0
     *
     * @apiParam {Long} parentId 父级ID，不传时默认为0
     *
     * @apiSuccess {Long} id 地区ID
     * @apiSuccess {Date} dateCreated 创建时间
     * @apiSuccess {Date} lastUpdated 更新时间
     * @apiSuccess {Long} version 版本
     * @apiSuccess {BigDecimal} firstPinyin 拼音首字母
     * @apiSuccess {BigDecimal} level 层级
     * @apiSuccess {BigDecimal} locationName 地方名
     * @apiSuccess {BigDecimal} parentId 父级ID
     * @apiSuccess {Integer} state 状态
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data": [
     *      {
     *          "id": 110101,
     *          "dateCreated": "2016-10-08 17:46:58",
     *          "lastUpdated": "2016-10-08 17:47:01",
     *          "version": 0,
     *          "firstPinyin": "D",
     *          "level": 3,
     *          "locationName": "东城区",
     *          "parentId": "110100",
     *          "state": 0
     *      },
     *          ...
     *      ]
     *  }
     */
    @RequestMapping(value=Routes.LOCATION_LIST,method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(LocationDto locationDto, HttpServletRequest request){
        RestDoing doing = jr ->{
            if (null == locationDto.getParentId()){
                locationDto.setParentId("0");
            }
            List<Location> locationList = locationRepository.findByParentId(locationDto.getParentId());
            jr.setData(locationList);
        };
        return doing.go(request,log);
    }
    /**
     * @api {get} /api/v2/location/show 查询地区
     * @apiGroup location
     * @apiVersion 1.0.0
     *
     * @apiParam {Long} id 地区ID
     *
     * @apiSuccess {Long} id 地区ID
     * @apiSuccess {Date} dateCreated 创建时间
     * @apiSuccess {Date} lastUpdated 更新时间
     * @apiSuccess {Long} version 版本
     * @apiSuccess {BigDecimal} firstPinyin 拼音首字母
     * @apiSuccess {BigDecimal} level 层级
     * @apiSuccess {BigDecimal} locationName 地方名
     * @apiSuccess {BigDecimal} parentId 父级ID
     * @apiSuccess {Integer} state 状态
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data": {
     *          "id": 110000,
     *          "dateCreated": "2016-10-08 17:46:58",
     *          "lastUpdated": "2016-10-08 17:47:01",
     *          "version": 0,
     *          "firstPinyin": "B",
     *          "level": 1,
     *          "locationName": "北京市",
     *          "parentId": "0",
     *          "state": 0
     *      }
     *  }
     */
    @RequestMapping(value=Routes.LOCATION_SHOW,method = RequestMethod.GET)
    @ResponseBody
    public JsonResult show(LocationDto locationDto, HttpServletRequest request){
        RestDoing doing = jr ->{
            if (null == locationDto.getId()){
                jr.remind("请输入正确的地区ID",log);
            }
            Location location = locationRepository.findOne(locationDto.getId());
            jr.setData(location);
        };
        return doing.go(request,log);
    }
}
